﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace MatrixDemo
{
    public partial class Form1 : Form
    {
        Matrix matrix;
        Point[] points;
        float dX, dY, angle;
        Point center;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen myPen = new Pen(Color.Blue, 1);
            matrix.TransformPoints(points);
            g.DrawPolygon(myPen, points);
            matrix.Reset();
        }

        void LoadForm()
        {
            dX = dY = 0;
            points = new Point[]  {
                 new Point(20, 20),
                 new Point(120, 20),
                 new Point(120, 120),
                 new Point(20, 120)
            };

            matrix = new Matrix();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadForm();
        }

        void GetValuesFromTextbox()
        {
            try
            {
                if (textBoxX.Text != "")
                    dX = (float)Convert.ToDouble(textBoxX.Text);
                if (textBoxY.Text != "")
                 dY = (float)Convert.ToDouble(textBoxY.Text);
                if (textBoxAngle.Text != "")
                    angle = (float)Convert.ToDouble(textBoxAngle.Text);
            }
            catch
            {
            }
        }

        private void buttonTranslate_Click(object sender, EventArgs e)
        {
            GetValuesFromTextbox();
            matrix.Translate(dX, dY);
            Invalidate();
        }

        private void buttonScale_Click(object sender, EventArgs e)
        {
            GetValuesFromTextbox();
            matrix.Scale(dX, dY, MatrixOrder.Append);
            Invalidate();
        }

        private void buttonShear_Click(object sender, EventArgs e)
        {
            GetValuesFromTextbox();
            matrix.Shear(dX, dY);
            Invalidate();
        }

        private void buttonRotate_Click(object sender, EventArgs e)
        {
            //for (int i = 0; i < points.Length; i++)
            //{
            //    center.X += points[i].X;
            //    center.Y += points[i].Y;
            //}

            //center.X /= 4;
            //center.Y /= 4;
            center.X = center.Y = 70;

            GetValuesFromTextbox();
            matrix.Translate(-center.X, -center.Y);
            matrix.Rotate(angle);
            matrix.Translate(center.X, center.Y);
            //matrix.RotateAt(angle, center);
            Invalidate();
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            LoadForm();
            Invalidate();
        }
    }
}
